//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "YoYInflationCapFloorEngine.h"
using namespace Cephei::QL::Pricingengines::Inflation;
#include <gen/QL/Indexes/YoYInflationIndex.h>
#include <gen/QL/Termstructures/Volatility/Inflation/YoYOptionletVolatilitySurface.h>
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Termstructures::Volatility::Inflation;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Inflation::CYoYInflationCapFloorEngine::CYoYInflationCapFloorEngine (boost::shared_ptr<QuantLib::YoYInflationCapFloorEngine>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phYoYInflationCapFloorEngine = NULL;
#endif
	_ppYoYInflationCapFloorEngine = &childNative;
    
}
Cephei::QL::Pricingengines::Inflation::CYoYInflationCapFloorEngine::CYoYInflationCapFloorEngine (QuantLib::YoYInflationCapFloorEngine& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phYoYInflationCapFloorEngine = NULL;
#endif
	_ppYoYInflationCapFloorEngine = new boost::shared_ptr<QuantLib::YoYInflationCapFloorEngine> (&childNative);
    
    _YoYInflationCapFloorEngineOwner = owner;
    
}

Cephei::QL::Pricingengines::Inflation::CYoYInflationCapFloorEngine::CYoYInflationCapFloorEngine (CYoYInflationCapFloorEngine^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phYoYInflationCapFloorEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppYoYInflationCapFloorEngine = new boost::shared_ptr<QuantLib::YoYInflationCapFloorEngine> (copy->GetShared());
        
    }
}
Cephei::QL::Pricingengines::Inflation::CYoYInflationCapFloorEngine::CYoYInflationCapFloorEngine (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phYoYInflationCapFloorEngine = NULL;
#endif
	if (!t->IsSubclassOf(CYoYInflationCapFloorEngine::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Inflation::CYoYInflationCapFloorEngine::CYoYInflationCapFloorEngine (QuantLib::Handle<QuantLib::YoYInflationCapFloorEngine>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phYoYInflationCapFloorEngine = &childNative;
	_ppYoYInflationCapFloorEngine = &static_cast<boost::shared_ptr<QuantLib::YoYInflationCapFloorEngine>>(childNative.currentLink());
    
    _YoYInflationCapFloorEngineOwner = owner;
}
Cephei::QL::Pricingengines::Inflation::CYoYInflationCapFloorEngine::CYoYInflationCapFloorEngine (QuantLib::Handle<QuantLib::YoYInflationCapFloorEngine> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phYoYInflationCapFloorEngine = &childNative;
	_ppYoYInflationCapFloorEngine = &static_cast<boost::shared_ptr<QuantLib::YoYInflationCapFloorEngine>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Inflation::CYoYInflationCapFloorEngine::CYoYInflationCapFloorEngine (QuantLib::YoYInflationCapFloorEngine childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phYoYInflationCapFloorEngine = NULL;
#endif
	_ppYoYInflationCapFloorEngine = new boost::shared_ptr<QuantLib::YoYInflationCapFloorEngine> (new QuantLib::YoYInflationCapFloorEngine (childNative));
    
}
#endif

Cephei::QL::Pricingengines::Inflation::CYoYInflationCapFloorEngine::~CYoYInflationCapFloorEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppYoYInflationCapFloorEngine != NULL)
    {
	    delete _ppYoYInflationCapFloorEngine;
        _ppYoYInflationCapFloorEngine = NULL;
    }
}
Cephei::QL::Pricingengines::Inflation::CYoYInflationCapFloorEngine::!CYoYInflationCapFloorEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppYoYInflationCapFloorEngine != NULL)
    {
	    delete _ppYoYInflationCapFloorEngine;
    }
}
QuantLib::YoYInflationCapFloorEngine& Cephei::QL::Pricingengines::Inflation::CYoYInflationCapFloorEngine::GetReference ()
{
    if (_ppYoYInflationCapFloorEngine == NULL) throw gcnew NativeNullException ();
	return **_ppYoYInflationCapFloorEngine;
}
boost::shared_ptr<QuantLib::YoYInflationCapFloorEngine>& Cephei::QL::Pricingengines::Inflation::CYoYInflationCapFloorEngine::GetShared ()
{
    if (_ppYoYInflationCapFloorEngine == NULL) throw gcnew NativeNullException ();
	return *_ppYoYInflationCapFloorEngine;
}
QuantLib::YoYInflationCapFloorEngine* Cephei::QL::Pricingengines::Inflation::CYoYInflationCapFloorEngine::GetPointer ()
{
    if (_ppYoYInflationCapFloorEngine == NULL) throw gcnew NativeNullException ();
	return &**_ppYoYInflationCapFloorEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::YoYInflationCapFloorEngine>& Cephei::QL::Pricingengines::Inflation::CYoYInflationCapFloorEngine::GetHandle ()
{
	if (_phYoYInflationCapFloorEngine == NULL)
	{
		_phYoYInflationCapFloorEngine = new Handle<QuantLib::YoYInflationCapFloorEngine> (*_ppYoYInflationCapFloorEngine);
	}
	return *_phYoYInflationCapFloorEngine;
}
#endif
bool Cephei::QL::Pricingengines::Inflation::CYoYInflationCapFloorEngine::HasNative () 
{
	return (_ppYoYInflationCapFloorEngine != NULL);
}

Cephei::QL::Pricingengines::Inflation::IYoYInflationCapFloorEngine^ Cephei::QL::Pricingengines::Inflation::CYoYInflationCapFloorEngine::Calculate::get ()
{
    try
    {
    	(*_ppYoYInflationCapFloorEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Indexes::IYoYInflationIndex^ Cephei::QL::Pricingengines::Inflation::CYoYInflationCapFloorEngine::Index::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::YoYInflationIndex> _rv = (boost::shared_ptr<QuantLib::YoYInflationIndex>)(*_ppYoYInflationCapFloorEngine)->index ( );   
        Cephei::QL::Indexes::CYoYInflationIndex^ _nrv = gcnew Cephei::QL::Indexes::CYoYInflationIndex (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Pricingengines::Inflation::IYoYInflationCapFloorEngine^ Cephei::QL::Pricingengines::Inflation::CYoYInflationCapFloorEngine::SetVolatility (Cephei::QL::Termstructures::Volatility::Inflation::IYoYOptionletVolatilitySurface^ vol)
{
    CYoYOptionletVolatilitySurface^ _Cvol;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cvol = safe_cast<CYoYOptionletVolatilitySurface^> (vol);
        _Cvol->Lock();
        Handle<QuantLib::YoYOptionletVolatilitySurface>& _vol = static_cast<Handle<QuantLib::YoYOptionletVolatilitySurface>&> (_Cvol->GetHandle ()); 
    	(*_ppYoYInflationCapFloorEngine)->setVolatility ( _vol );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cvol != nullptr) _Cvol->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Inflation::IYoYOptionletVolatilitySurface^ Cephei::QL::Pricingengines::Inflation::CYoYInflationCapFloorEngine::Volatility::get ()
{
    try
    {
    	Handle<QuantLib::YoYOptionletVolatilitySurface> _rv = (Handle<QuantLib::YoYOptionletVolatilitySurface>)(*_ppYoYInflationCapFloorEngine)->volatility ( );   
        Cephei::QL::Termstructures::Volatility::Inflation::CYoYOptionletVolatilitySurface^ _nrv = gcnew Cephei::QL::Termstructures::Volatility::Inflation::CYoYOptionletVolatilitySurface (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

